//
//  SetInputView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/16.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation
class SetInputView: NSView {
    
    var saveBlock:ResultString?
    
    var isOnlyNumber:Bool = false
    var maxCount:Int = 6
    
    var isEditing:Bool = false {
        didSet {
            if isEditing {
                self.editLab.stringValue = self.noEditLab.stringValue
            }else {
                self.noEditLab.stringValue = self.editLab.stringValue
            }
            self.editButton.isHidden = isEditing
            self.editBoard.isHidden = !isEditing
            self.saveButton.isHidden = !isEditing
        }
    }
    
    
    var _text:String!
    var text:String? {
        didSet {
            self.noEditLab.stringValue = text ?? ""
            self.isEditing = false
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
    }
    
    convenience init(text:String,saveBlock:ResultString?) {
        self.init()
        self._text = text
        self.saveBlock = saveBlock
        self.setupUI()
        self.loadData()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func loadData() {
         if let t = self._text {
            self.noEditLab.stringValue = t
            self.editLab.stringValue = t
        }
    }
    
    func setupUI() {
        
        self.addSubview(editButton)
        editButton.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalToSuperview().offset(-4)
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        editButton.addSubview(noEditLab)
        noEditLab.snp.makeConstraints { (make) in
            make.height.equalTo(18)
            make.centerX.equalToSuperview().offset(-5)
            make.centerY.equalToSuperview().offset(-1)
            make.width.lessThanOrEqualTo(100)
        }
        
        editButton.addSubview(noEditTipLab)
        noEditTipLab.snp.makeConstraints { (make) in
            make.height.equalTo(12)
            make.right.equalTo(-15)
            make.bottom.equalTo(noEditLab)
        }
        
        self.addSubview(saveButton)
        saveButton.snp.makeConstraints { (make) in
            make.width.equalTo(40)
            make.height.equalTo(20)
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        self.addSubview(editBoard)
        editBoard.snp.makeConstraints { (make) in
            make.height.equalToSuperview().offset(-4)
            make.left.equalToSuperview()
            make.right.equalTo(saveButton.snp.left).offset(-3)
            make.centerY.equalToSuperview()
        }
        
        editBoard.addSubview(editLab)
        editLab.snp.makeConstraints { (make) in
            make.height.equalTo(18)
            make.left.equalToSuperview().offset(5)
            make.right.equalToSuperview().offset(-5)
            make.centerY.equalToSuperview()
        }
    }
    
    
    lazy var editButton: ZHButton = {
        let btn = ZHButton.init()
        btn.zh_backgroundColor = color_gray(220)
        btn.hasBorder = true
        btn.zh_cornerRadius = 6
        btn.target = self
        btn.action = #selector(editButtonAction(senber:))
        return btn
    }()
    
    lazy var noEditLab: NSTextField = {
        let lab = NSTextField.init()
        lab.wantsLayer = true
        lab.backgroundColor = NSColor.clear
        lab.isBordered = false
        lab.isEditable = false
        lab.stringValue = "..."
        lab.maximumNumberOfLines = 1
        lab.font = NSFont.systemFont(ofSize: 14)
        lab.textColor = color_gray(60)
        lab.focusRingType = .none
        lab.sizeToFit()
        return lab
    }()
    
    
    lazy var noEditTipLab: NSTextField = {
        let lab = NSTextField.init()
        lab.wantsLayer = true
        lab.backgroundColor = NSColor.clear
        lab.isBordered = false
        lab.isEditable = false
        lab.stringValue = "(可编辑)"
        lab.maximumNumberOfLines = 1
        lab.font = NSFont.systemFont(ofSize: 11)
        lab.textColor = color_gray(160)
        lab.focusRingType = .none
        lab.sizeToFit()
        return lab
    }()
    
    
//    lazy var noEditTipLab: NSImageView = {
//        let imageV = NSImageView.init(image: NSImage.init())
//        if let image = NSImage.image(name: "icon_changeName") {
//            imageV.image = image
//        }
//        return imageV
//    }()
    
    ////////////////////////////
    
    lazy var editBoard: NSView = {
        let view = NSView.init()
        view.wantsLayer = true
        view.layer?.cornerRadius = 6
        view.layer?.masksToBounds = true
        view.layer?.borderWidth = 1
        view.layer?.borderColor = color_main108.cgColor
        view.isHidden = true
        return view
    }()
    
    lazy var editLab: NSTextField = {
        let lab = NSTextField.init()
        lab.maximumNumberOfLines = 1
        lab.wantsLayer = true
        lab.backgroundColor = NSColor.clear
        lab.font = NSFont.systemFont(ofSize: 14)
        lab.textColor = color_gray(0)
        lab.placeholderString = "请输入"
        lab.isBordered = false
        lab.focusRingType = .none
        lab.cell?.lineBreakMode = .byTruncatingTail
        lab.cell?.truncatesLastVisibleLine = true
        lab.cell?.usesSingleLineMode = true
        lab.delegate = self
        return lab
    }()
    
    
    lazy var saveButton: ZHButton = {
        let btn = ZHButton.init()
        btn.zh_title = "保存"
        btn.zh_font = NSFont.systemFont(ofSize: 12)
        btn.zh_color = color_gray(255)
        btn.zh_backgroundColor = color_main108
        btn.hasBorder = true
        btn.zh_cornerRadius = 6
        btn.isHidden = true
        btn.target = self
        btn.action = #selector(editButtonAction(senber:))
        return btn
    }()
    
    @objc func editButtonAction(senber:ZHButton?) {
        self.isEditing = !self.isEditing
        if self.isEditing == false {
            if saveBlock != nil {
                let text = self.noEditLab.stringValue
                self.saveBlock!(text)
            }
        }
    }
}

extension SetInputView:NSTextFieldDelegate {
    func controlTextDidChange(_ obj: Notification) {
        if let textField = obj.object as? NSTextField {
            if self.isOnlyNumber {
                let string = textField.stringValue
                let csIn = "0123456789"
                var stringResult:String = ""
                for index in 0..<string.count {
                    let c = string[index]
                    if csIn.contains(c) {
                        stringResult += c
                    }
                }
                self.editLab.stringValue = stringResult.substring(toIndex: self.maxCount)
            }else {
                self.editLab.stringValue = editLab.stringValue.substring(toIndex: self.maxCount)
            }
            
            /*
            let cs = CharacterSet.init(charactersIn: csIn)
            let filtered = string.components(separatedBy: cs).joined(separator: "")
            print("filtered \(filtered)")
             */
        }
    }
}
